<?php
namespace Admin\Model;
use Think\Model;
class CustomerXufeiModel extends Model {
	//续费客户管理

	public function index($arr,$is_admin){
		$uinfo = UserModel::loadFromSession();
		$CustomerTagList = M("customer_tag_list");
		if (!empty($arr['login_name'])) {
			$xwhere['customer.nickname'] = $arr['login_name'];
 			$xwhere['customer.phone'] = $arr['login_name'];
 			$xwhere['customer.weixin_id'] = $arr['login_name'];
 			$xwhere['_logic'] = 'OR';
 			$customer_message = M("customer")->field("customer_id")->where($xwhere)->find();
			$where['customer_id'] = $customer_message['customer_id'];
			$wheres['customer_xufei.customer_type'] = array(array('gt',0),array('lt',8));
		}

		if(!empty($arr['tag_name'])){
			$customer_tag = M("customer_tag");
			$search_tag = $arr['tag_name'];
			$w['tag_name'] = array('like','%'.$search_tag .'%');
			$w_tag  = $customer_tag ->field("tag_id")->where($w)->find();
			if($w_tag['tag_id']){
				if($uinfo['user_id']!="1"){
					$pwhere['user_id'] = $uinfo['user_id'];
				}
				$pwhere['tag_id'] = $w_tag['tag_id'];
				$results = $CustomerTagList->field("customer_id")->where($pwhere)->select();
			}
			$wheres['customer_xufei.customer_type'] = array(array('gt',0),array('lt',8));
		}

		if(!empty($arr['create_time'])){
			$create_time = $arr['create_time'];
			$wheres['customer.create_time'] = array('like','%'.$create_time.'%');
			$wheres['customer_xufei.customer_type'] = array(array('gt',0),array('lt',8));
		}

		if(!empty($arr['id']) && !empty($arr['customer_type'])){
			$wheres['customer_xufei.customer_type'] = $arr['customer_type'];
			$wheres['end_time'] =array('eq',"");
		}elseif(empty($arr['id']) && !empty($arr['customer_type'])){
			$wheres['customer_xufei.customer_type'] = $arr['customer_type'];
			$wheres['end_time'] =array('eq',"");
		}else{
			if($arr['id'] == "9"){
				$wheres['customer_xufei.customer_type'] = array(array('gt',0),array('lt',8));
			}else{
				$wheres['customer_xufei.customer_type'] = $arr['id'];
			}
		}

		if($uinfo['user_id'] == "1"){
		 	$where['_string'] = "1=1";
		 }else{
		 	$where['user_id'] = $uinfo['user_id'];
		 }
		$customer = M("customer_xufei")->where($where)->order("customer_flag desc,customer_xufei_id desc")->select();
		foreach ($customer as &$cval) {
			if(empty($cval['end_time'])){
				$new_customer[] = $cval;
			}
		}
		
		$Xufei = M("customer_xufei");
		foreach ($new_customer as $key => $value) {
			$wheres['customer_xufei.customer_id'] = $value['customer_id'];
			$list[] =  $Xufei
			     ->field("user.user_id,user.nickname as user_name,customer.customer_id,customer.nickname,customer.create_time,customer.location,customer.mount,customer.note,customer.sales_person,customer_xufei.customer_type,customer_xufei.customer_xufei_id,customer_xufei.customer_flag,customer_xufei.end_time")
				->join("customer ON customer_xufei.customer_id = customer.customer_id","LEFT")
				->join("user ON customer_xufei.user_id = user.user_id","LEFT")
				->where($wheres)
				->find();
		}
		//return $list;
		$now_time = time();
		foreach ($list as $vlist) {
			if(strtotime($vlist['end_time']) ==""){
				$new_list[] = $vlist;
			}
		}
	
		foreach ($new_list as &$val) {
			$swhere['customer_id'] = $val['customer_id'];
			$val['tags'] =  $CustomerTagList
					->field("customer_tag.tag_name,customer_tag.tag_id")
					->join("customer_tag ON customer_tag_list.tag_id = customer_tag.tag_id")
					->where($swhere)
					->select();
			$count = count($val['tags']);
			if($count == "0"){
				$val['tags'] = "";
			}elseif($count == "1"){
				$val['tags'] = $val['tags']['0']['tag_name'];
			}else{
				foreach ($val['tags'] as &$vt) {
					$vt = $vt['tag_name'];
				}
				$val['tags'] = implode($val['tags'],'、');
			}
		}
		if(!empty($arr['tag_name'])){
    	 	//标签搜索过滤
			foreach ($new_list as $b5) {
				foreach ($results as $rval) {
					if($b5['customer_id'] == $rval['customer_id']){
						$TagArray[] = $b5;
					}
				}
			}
			$new_list = $TagArray;
		}
	
    	$count = count($new_list);
		$Page = new \Common\ORG\Page( $count, $number );
		//分页
		for($i=($arr['p']-1)*$arr['psize'];$i<$arr['p']*$arr['psize'];$i++){
    	 	$xufei_list[$i] = $new_list[$i];
    	 }
    	 //过滤空值、查找标签
    	foreach ($xufei_list as $xufei_key => $xufei_value) {
    	 	if(!empty($xufei_value)){
    	 		$xufei_value['create_time'] = substr($xufei_value['create_time'],0,10);
    	 		$s_list[] = $xufei_value;
    	 	}
    	}
		if( $s_list){
			$res['rcode'] = 1;
			$res['list'] = $s_list;
			$res['getdata'] = $arr;
			$res['page'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $arr['psize'];
		}
		
		return $res;
	}

}